//
//  HPHomeMViewController.swift
//  TLenPoint
//
//  Created by Docker on 2018/3/22.
//  Copyright © 2018年 Docker. All rights reserved.
//

import UIKit

class HPHomeMViewController: UIViewController {

    //  引用ViewModel
    fileprivate lazy var homeViewModel: HPHomeViewModel = HPHomeViewModel()
    fileprivate lazy var tableView : UITableView = {
        let table = UITableView(frame: CGRect(x: 0, y: 64, width: self.view.frame.size.width, height: self.view.frame.size.height - 64), style: UITableViewStyle.plain)
        table.delegate = self
        table.dataSource = self
        table.register(UITableViewCell.self, forCellReuseIdentifier: LPHomeTabelViewCell)
        table.register(HPHomeOneTableViewCell.self, forCellReuseIdentifier: HPHomeOneTableViewCellIdetifer)
        table.register(HPSimulaTionTableViewCell.self, forCellReuseIdentifier: HPHomeSimulationCellIdetifer)
        table.register(HPTwoTableViewCell.self, forCellReuseIdentifier: HPTwoTableViewCellIdetifer)
        return table
    }()
    // MARK: -- 初始化方法
    override func viewDidLoad() {
        super.viewDidLoad()
       self.view.backgroundColor = UIColor.white
       self.view.addSubview(tableView)
       self.dataReult()
       
    }
  
    private func dataReult(){
        self.homeViewModel.RestData { (result, string) in
            if result == true{
               self.tableView.reloadData()
            }
        }
        self.homeViewModel.RestHomeGameData(feeType: "1") { (result, string) in
            if result == true{
                self.tableView.reloadData()
            }
        }
    }


}
// MARK : UITabelView代理
extension HPHomeMViewController:UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 0){
            return 2
        }else{
            return self.homeViewModel.simulationList.count
        }
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.section == 0 ){
            if(indexPath.row == 0){
                let cell = tableView.dequeueReusableCell(withIdentifier: HPHomeOneTableViewCellIdetifer, for: indexPath) as! HPHomeOneTableViewCell
                cell.array = self.homeViewModel.rotato
                return cell
            }else{
                let cell = tableView.dequeueReusableCell(withIdentifier: HPTwoTableViewCellIdetifer, for: indexPath) as! HPTwoTableViewCell
//                cell.array = self.homeViewModel.rotato
                return cell
            }
          
        }else{
          let cell = tableView.dequeueReusableCell(withIdentifier: HPHomeSimulationCellIdetifer, for: indexPath) as! HPSimulaTionTableViewCell
            cell.model = self.homeViewModel.simulationList[indexPath.row]
            return cell
        }

    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
        if (section == 1){
            return 60
        }else{
            return 1
        }
       
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        if (indexPath.section == 0){
            if(indexPath.row == 0){
               return 200
            }else{
                return 114
            }
           
        }else{
            return 130
        }
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
        let view = UIView(frame: CGRect(x: 0, y: 0, width: ScranWidth, height: 60))
//        view.backgroundColor = UIColor.red
        return view
    }
}
